package com.zhu.shopcenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.zhu.shopcenter.mapper.TradeOrderMapper;
import com.zhu.shopcenter.pojo.TradeOrder;
import com.zhu.shopcenter.service.PayService;
import com.zhu.shopcenter.zhifubao.AliPay;
import com.zhu.shopcenter.zhifubao.AlipayBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;


@Service
@Slf4j
public class PayServiceImpl implements PayService {
    @Autowired
    private AliPay aliPay;
    @Autowired
    private TradeOrderMapper tradeOrderMapper;

    @Override
    public String pay(String orderId) throws AlipayApiException {
        TradeOrder tradeOrder = tradeOrderMapper.selectById(orderId);
        AlipayBean alipayBean=new AlipayBean();
        alipayBean.setOut_trade_no(tradeOrder.getOrderNo());
        alipayBean.setProduct_code("FAST_INSTANT_TRADE_PAY");
        BigDecimal totalFee = tradeOrder.getTotalFee();
        BigDecimal divide = totalFee.divide(new BigDecimal(100));
        alipayBean.setTotal_amount(String.valueOf(divide));
        alipayBean.setSubject(tradeOrder.getCourseTitle());
        log.info("请求的数据：{}", JSON.toJSONString(alipayBean));
        return aliPay.pay(alipayBean);
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal=new BigDecimal(100);
        BigDecimal divide = bigDecimal.divide(new BigDecimal(100));
        System.out.println(divide);
    }
}
